<template>
  <div>
    <el-tree
      ref="treeRef"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      :default-expand-all="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-button @click="hClose">取消</el-button>
    <el-button @click="hSave">确定</el-button>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { getPermissionDetail, savePermission } from '@/api/settings'
import { arr2tree } from '@/utils/index'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 获取权限点渲染成树状数据
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = arr2tree(res.data, '0')
      const res1 = await getPermissionDetail(this.curId)
      this.$refs.treeRef.setCheckedKeys(res1.data.permIds)
    },
    hClose() {
      this.$emit('close')
    },
    async hSave() {
      const permIds = this.$refs.treeRef.getCheckedKeys()
      await savePermission({ id: this.curId, permIds })
      this.$emit('success')
    }
  }
}
</script>

<style>

</style>
